﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.UI;
using TaxBiz.SysConfig;
using TaxBiz.Tax;

namespace Admin.ALarm
{
    public partial class Query : BaseCommon.BasePage
    {
        protected int pageSize = 10;
        protected int page = 1;
        protected int allCount = 0;
        protected string pageUrl = string.Empty;
        protected string strWhere = string.Empty;
        protected int year = 0;
        protected string certificateID = string.Empty;//名称
        protected string houseType = string.Empty; //类型
        protected string departmentID = string.Empty;
        protected DataTable dtDepartmet = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            this.InitData();
            //初始化绑定
            dtDepartmet = new DepartmentBiz().GetList();
            string action = PageContext.GetRequest("action");
            if (!string.IsNullOrEmpty(action))
            {
                if (action.Trim().ToLower() == "query")
                {
                    this.BindData();
                }
                else if (action.ToLower() == "download")
                {
                    this.DownLoad();
                }
            }
        }

        protected void InitData()
        {
            pageUrl = Request.Url.AbsolutePath;
            //当前页码
            if (PageContext.GetRequest("page") != string.Empty)
            {
                int.TryParse(PageContext.GetRequest("page"), out page);
            }
            pageUrl += "?page=" + page;
            pageUrl += "&action=query";
            //名称
            certificateID = PageContext.GetRequest("txtCertificateID");
            if (certificateID != string.Empty)
            {
                pageUrl += "&txtCertificateID=" + certificateID;
                strWhere += " and certificateID = '" + certificateID + "'";
            }
            if (PageContext.GetRequestInt("selYear").HasValue)
            {
                year = PageContext.GetRequestInt("selYear").Value;
                pageUrl += "&selYear=" + year;
                strWhere += " and year = " + year;
            }
            departmentID = PageContext.GetRequest("selDepart");
            if (departmentID != string.Empty)
            {
                pageUrl += "&selDepart=" + departmentID;
                strWhere += " and departmentID = " + departmentID + "";
            }
            //只能查询本部门数据权限
            if (isQueryDepartmentOnly)
            {
                strWhere += " and departmentID = " + LoginDepartmentID + "";
            }
            houseType = PageContext.GetRequest("selHouseType");
            if (houseType != string.Empty)
            {
                pageUrl += "&selHouseType=" + houseType;
                strWhere += " and houseType = '" + houseType + "'";
            }
        }

        protected void BindData()
        {
            this.rptDataList.DataSource = AlarmBiz.GetList(strWhere, pageSize, page, ref allCount);
            this.rptDataList.DataBind();
            //设置分页参数
            this.pageCountrol.CurPage = page;
            this.pageCountrol.PageSize = pageSize;
            this.pageCountrol.AllCount = allCount;
            this.pageCountrol.PageUrl = pageUrl;
            this.pageCountrol.PageName = "page";
        }

        //下载报表
        protected void DownLoad()
        {
            string fileName = "预警信息" + DateTime.Now.ToString("yyyyMMdd");
            string[] titles = new string[] { "税款属期", "所属部门", "管理员", "房产证", "房主姓名", "手机号", "应缴税种", "实缴税种", "应缴金额", "实缴金额", "应补金额", "承租姓名", "承租手机", "门市名称" };
            string[] fields = new string[] { "Year", "DepartmentName", "ManagerName", "CertificateID", "Name", "MobileCode", "RequireTax", "CompleteTax", "TotalTax", "bTotalTax", "baTotal", "RentName", "RentMobileCode", "ShopName" };

            int tempAllCount = 0;
            DataTable dt = AlarmBiz.GetList(strWhere, 1000, 1, ref tempAllCount);
            if (dt != null)
            {
                dt.Columns.Add("baTotal");
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["RequireTax"] != null && dr["RequireTax"] != DBNull.Value)
                    {
                        dr["RequireTax"] = dr["RequireTax"].ToString().Replace("<br/>", ",");
                    }
                    if (dr["CompleteTax"] != null && dr["CompleteTax"] != DBNull.Value)
                    {
                        dr["CompleteTax"] = dr["CompleteTax"].ToString().Replace("<br/>", ",");
                    }
                    dr["baTotal"] = Decimal.Parse(dr["TotalTax"].ToString()) - Decimal.Parse(dr["bTotalTax"].ToString());
                }
            }
            if (dt == null)
            {
                return;
            }

            ToExcel(dt, fileName, titles, fields);
        }

    }
}